//
//  LBViewController.m
//  LittleBall
//
//  Created by apple on 11-11-20.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "LBMoveAnimationDelegate.h"
#import "LittleBallPlayViewController.h"


@implementation LBMoveAnimationDelegate

@synthesize m_owner;

-(id)initWithPath:(NSMutableArray *)path ball:(LBBall *)ball owner:(id)owner;
{
    m_path = path;
    m_pathPointsCount = 0;
    m_pathPointsTotalCount = [path count];
    m_ball = ball;
    m_owner = owner;
    
    
    if( !(self = [super init]))
    {
        return nil;
    }
    
    [UIView beginAnimations:nil context:NULL];
    
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector: @selector(animationDidStop: finished: context:)];
    
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:BALL_MOVE_SPEED];
    
    NSLog(@"array length: %d, objectAtIndex:%d, File:%s, Line:%d",[m_path count],m_pathPointsCount,__FILE__,__LINE__);
    LBPosition *position = [m_path objectAtIndex:m_pathPointsCount];
    ball.m_imageView.center = [LBPathFinder translatePositionToCenterPointFromRow:position.m_row column:position.m_column];
    
    m_pathPointsCount++;
    
    [UIView commitAnimations];
    
    return self;
}

- (void)animationWillStart:(NSString *)animationID context:(void *)context 
{
    // do nothing...    
}

- (void)animationDidStop:(NSString *)animationID finished: (NSNumber *)finished context:(void *)context
{
    if( m_pathPointsCount < m_pathPointsTotalCount )
    {
        [UIView beginAnimations:nil context:NULL];
        
        [UIView setAnimationDelegate:self];
        
        [UIView setAnimationRepeatCount:1];
        [UIView setAnimationDuration:BALL_MOVE_SPEED];
        
        NSLog(@"array length: %d, objectAtIndex:%d, File:%s, Line:%d",[m_path count],m_pathPointsCount,__FILE__,__LINE__);
        LBPosition *position = [m_path objectAtIndex:m_pathPointsCount];
        m_ball.m_imageView.center = [LBPathFinder translatePositionToCenterPointFromRow:position.m_row column:position.m_column];
        m_pathPointsCount++;
        
        [UIView commitAnimations];
    }
    else
    {
        BOOL indicator = [m_owner clearBallsWithBall:m_ball];
        
        if (!indicator)
        {
            if ( [m_owner getGameMode] == TRADITONAL_MODE )
            {
                [m_owner generateNewBalls];
            }
        }
        
        [m_owner setFlag:NO];
    }
}


@end
